/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.hokoblurdemo.ability.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.agp.components.element.PixelMapElement;
import ohos.media.image.PixelMap;

import com.example.hokoblurdemo.ability.ResourceTable;
import com.example.hokoblurdemo.ability.Utils.PixelMapUtil;

import com.hoko.blur.HokoBlur;
import com.hoko.blur.anno.Mode;
import com.hoko.blur.processorsecond.StackBlurManager;
import com.hoko.blur.task.AsyncBlurTask;

/**
 * @since 2021-08-04
 */
public class EasyBlurAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_abliity_easy_blur);
        initView();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void initView() {
        PixelMap bitmap = PixelMapUtil.getPixelMapFromResource(getContext(),ResourceTable.Media_cat);

        final Image imageView = (Image)findComponentById(ResourceTable.Id_image);
        final Image imageView1 = (Image)findComponentById(ResourceTable.Id_image1);
        final Image imageView2 = (Image)findComponentById(ResourceTable.Id_image2);
        final Image imageView3 = (Image)findComponentById(ResourceTable.Id_image3);

        imageView.setBackground(new PixelMapElement(bitmap));
        // java
        StackBlurManager _stackBlurManager = new StackBlurManager(bitmap);
        _stackBlurManager.process(20);
        imageView1.setBackground(new PixelMapElement(HokoBlur.with(this)
            .forceCopy(true)
            .translateX(150)
            .translateY(150)
            .scheme(HokoBlur.SCHEME_JAVA)
            .sampleFactor(3.0f)
            .radius(20)
            .blur(bitmap)));

        HokoBlur.with(this)
            .scheme(HokoBlur.SCHEME_NATIVE)
            .mode(Mode.MODE_GAUSSIAN)
            .translateX(250)
            .translateY(250)
            .forceCopy(false)
            .sampleFactor(5.0f)
            .needUpscale(true)
            .asyncBlur(bitmap, new AsyncBlurTask.Callback() {
                @Override
                public void onBlurSuccess(PixelMap bitmap) {
                    imageView2.setBackground(new PixelMapElement(bitmap));
                }

                @Override
                public void onBlurFailed(Throwable e) {
                    e.printStackTrace();
                }
            });

        HokoBlur.with(this)
            .scheme(HokoBlur.SCHEME_JAVA)
            .mode(Mode.MODE_BOX)
            .translateX(200)
            .translateY(200)
            .asyncBlur(bitmap, new AsyncBlurTask.Callback() {
                @Override
                public void onBlurSuccess(PixelMap bitmap) {
                    imageView3.setBackground(new PixelMapElement(bitmap));
                }

                @Override
                public void onBlurFailed(Throwable e) {
                    e.printStackTrace();
                }
            });

    }


}

